using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using THSLib;
using THSERP.BusinessEntities;
namespace THSERP.BaseProvider
{
    public partial class uiInformation : DevExpress.XtraEditors.XtraUserControl
    {
        public BaseModuleERP _module;        
        public uiInformation()
        {
            InitializeComponent();
        }

        public uiInformation(BaseModuleERP mdl)
        {
            InitializeComponent();
            _module = mdl;
        }


        public BaseModuleERP Module
        {
            get
            {
                return _module;
            }
            set
            {
                _module = value;
            }
        }

        public double TotalGrossAmount
        {
            get
            {
                try
                {
                    return Convert.ToDouble(fld_txtTotalGrossAmount.Text);
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        #region Utility Functions
        
        public int GetCurrentProduct()
        {
            try
            {
                int iICProductID = 0;
                
                String strMainTableName = THSUtil.GetTableNameFromBusinessObject(Module.CurrentModuleEntity.MainObject);
                String strItemTableName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Items";
                iICProductID = Convert.ToInt32(THSDbUtil.GetPropertyValue(Module.CurrentModuleEntity.ModuleObjects[strItemTableName], "FK_ICProductID"));

                return iICProductID;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public int GetCurrentMAStockSlotID()
        {
            try
            {
                int iICStockSlotID = 0;

                
                String strMainTableName = THSUtil.GetTableNameFromBusinessObject(Module.CurrentModuleEntity.MainObject);
                String strItemTableName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Items";
                iICStockSlotID = Convert.ToInt32(THSDbUtil.GetPropertyValue(Module.CurrentModuleEntity.ModuleObjects[strItemTableName], "FK_ICStockSlotID"));

                return iICStockSlotID;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public int GetCurrentItemPosition()
        {
            try
            {
                int iPos = 0;
                String strMainTableName = THSUtil.GetTableNameFromBusinessObject(Module.CurrentModuleEntity.MainObject);
                String strItemTableName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Items";
                iPos = Module.CurrentModuleEntity.ModuleObjects[strItemTableName].Position;

                return iPos;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        #endregion
        

        #region Information functions
        #region Postion Information
#if never
        private void CalculatePositionInformationForProposal(int iPosition)
        {
            Modules.Proposal.ProposalEntities entity = (Modules.Proposal.ProposalEntities)Module.CurrentModuleEntity;
            FAProposalItemsInfo objFAProposalItemsInfo = entity.FAProposalItemsEntityList[iPosition].SetToFAProposalItemsObject();
            double dbItemPrice = objFAProposalItemsInfo.FAProposalItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFAProposalItemsInfo.FAProposalItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;
            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFAProposalItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFAProposalItemsInfo.FAProposalItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }

        private void CalculatePositionInformationForSellOrder(int iPosition)
        {
            Modules.SellOrder.SellOrderEntities entity = (Modules.SellOrder.SellOrderEntities)Module.CurrentModuleEntity;
            FASellOrderItemsInfo objFASellOrderItemsInfo = entity.FASellOrderItemsEntityList[iPosition].SetToFASellOrderItemsObject();
            double dbItemPrice = objFASellOrderItemsInfo.FASellOrderItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFASellOrderItemsInfo.FASellOrderItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;
            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFASellOrderItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFASellOrderItemsInfo.FASellOrderItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }

        private void CalculatePositionInformationForCommission(int iPosition)
        {
            Modules.Commission.CommissionEntities entity = (Modules.Commission.CommissionEntities)Module.CurrentModuleEntity;
            FACommissionItemsInfo objFACommissionItemsInfo = entity.FACommissionItemsEntityList[iPosition].SetToFACommissionItemsObject();
            double dbItemPrice = objFACommissionItemsInfo.FACommissionItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFACommissionItemsInfo.FACommissionItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;

            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFACommissionItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFACommissionItemsInfo.FACommissionItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }

        private void CalculatePositionInformationForShipment(int iPosition)
        {
            Modules.Shipment.ShipmentEntities entity = (Modules.Shipment.ShipmentEntities)Module.CurrentModuleEntity;
            FAShipmentItemsInfo objFAShipmentItemsInfo = entity.FAShipmentItemsEntityList[iPosition].SetToFAShipmentItemsObject();
            double dbItemPrice = objFAShipmentItemsInfo.FAShipmentItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFAShipmentItemsInfo.FAShipmentItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;

            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFAShipmentItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFAShipmentItemsInfo.FAShipmentItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }

        private void CalculatePositionInformationForInvoice(int iPosition)
        {
            Modules.Invoice.InvoiceEntities entity = (Modules.Invoice.InvoiceEntities)Module.CurrentModuleEntity;
            FAInvoiceItemsInfo objFAInvoiceItemsInfo = entity.FAInvoiceItemsEntityList[iPosition].SetToFAInvoiceItemsObject();
            double dbItemPrice = objFAInvoiceItemsInfo.FAInvoiceItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFAInvoiceItemsInfo.FAInvoiceItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;

            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFAInvoiceItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFAInvoiceItemsInfo.FAInvoiceItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }

        private void CalculatePositionInformationForSellReturn(int iPosition)
        {
            Modules.SellReturn.SellReturnEntities entity = (Modules.SellReturn.SellReturnEntities)Module.CurrentModuleEntity;
            FASellReturnItemsInfo objFASellReturnItemsInfo = entity.FASellReturnItemsEntityList[iPosition].SetToFASellReturnItemsObject();
            double dbItemPrice = objFASellReturnItemsInfo.FASellReturnItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFASellReturnItemsInfo.FASellReturnItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;
            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFASellReturnItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFASellReturnItemsInfo.FASellReturnItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }

        private void CalculatePositionInformationForCreditNote(int iPosition)
        {
            Modules.CreditNote.CreditNoteEntities entity = (Modules.CreditNote.CreditNoteEntities)Module.CurrentModuleEntity;
            FACreditNoteItemsInfo objFACreditNoteItemsInfo = entity.FACreditNoteItemsEntityList[iPosition].SetToFACreditNoteItemsObject();
            double dbItemPrice = objFACreditNoteItemsInfo.FACreditNoteItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFACreditNoteItemsInfo.FACreditNoteItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;

            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFACreditNoteItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFACreditNoteItemsInfo.FACreditNoteItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }

        private void CalculatePositionInformationForPurchaseOrder(int iPosition)
        {
            Modules.PurchaseOrder.PurchaseOrderEntities entity = (Modules.PurchaseOrder.PurchaseOrderEntities)Module.CurrentModuleEntity;
            FAPurchaseOrderItemsInfo objFAPurchaseOrderItemsInfo = entity.FAPurchaseOrderItemsEntityList[iPosition].SetToFAPurchaseOrderItemsObject();
            double dbItemPrice = objFAPurchaseOrderItemsInfo.FAPurchaseOrderItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFAPurchaseOrderItemsInfo.FAPurchaseOrderItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;
            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFAPurchaseOrderItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFAPurchaseOrderItemsInfo.FAPurchaseOrderItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }

        private void CalculatePositionInformationForReceipt(int iPosition)
        {
            Modules.Receipt.ReceiptEntities entity = (Modules.Receipt.ReceiptEntities)Module.CurrentModuleEntity;
            FAReceiptItemsInfo objFAReceiptItemsInfo = entity.FAReceiptItemsEntityList[iPosition].SetToFAReceiptItemsObject();
            double dbItemPrice = objFAReceiptItemsInfo.FAReceiptItemPrice;
            double dbItemProductDiscountAmount = dbItemPrice * objFAReceiptItemsInfo.FAReceiptItemProductDiscount / 100;
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;

            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;

            int iMAProductID = objFAReceiptItemsInfo.FAProductIDCombo;
            MAProductsInfo objMAProductsInfo = (MAProductsInfo)new MAProductsController().GetObjectByID(iMAProductID);
            if (objMAProductsInfo != null)
            {
                dbBuyingPrice += objMAProductsInfo.MAProductSupplierPrice * objFAReceiptItemsInfo.FAReceiptItemProductQty;
            }
            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }
#endif
        public void CalculatePositionInformation(int iPosition)
        {
            
            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(Module.CurrentModuleEntity.MainObject);
            String strItemTableName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Items";
            String strColumnName = String.Empty;
            
            //Get Item Price
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "Price";
            double dbItemPrice = THSUtil.Round(Convert.ToDouble(THSDbUtil.GetPropertyValue(Module.CurrentModuleEntity.ModuleObjects[strItemTableName], strColumnName)),THSApp.PriceDecimal);

            //Get Item Product Discount
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductDiscount";
            double dbItemProductDiscount = Convert.ToDouble(THSDbUtil.GetPropertyValue(Module.CurrentModuleEntity.ModuleObjects[strItemTableName], strColumnName));

            //Get Item Product Qty
            strColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductQty";
            double dbItemProductQty = Convert.ToDouble(THSDbUtil.GetPropertyValue(Module.CurrentModuleEntity.ModuleObjects[strItemTableName], strColumnName));

            double dbItemProductDiscountAmount = THSUtil.Round(dbItemPrice * dbItemProductDiscount/ 100,THSApp.PriceDecimal);
            double dbItemNetAmount = dbItemPrice - dbItemProductDiscountAmount;
            double dbBuyingPrice = 0;
            double dbMarginPercent = 0;



            int iICProductID = Convert.ToInt32(THSDbUtil.GetPropertyValue(Module.CurrentModuleEntity.ModuleObjects[strItemTableName], "FK_ICProductID"));

            ICProductsInfo objICProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(iICProductID);
            if (objICProductsInfo != null)
            {
                dbBuyingPrice += objICProductsInfo.ICProductSupplierPrice * dbItemProductQty;
            }

            dbBuyingPrice = THSUtil.Round(dbBuyingPrice, THSApp.PriceDecimal);

            dbMarginPercent = (dbItemNetAmount - dbBuyingPrice) * 100 / dbItemNetAmount;

            fld_txtItemPrice.Text = dbItemPrice.ToString();
            fld_txtItemProductDiscountAmount.Text = dbItemProductDiscountAmount.ToString();
            fld_txtItemNetAmount.Text = dbItemNetAmount.ToString();
            fld_txtBuyingPrice.Text = dbBuyingPrice.ToString();
            fld_txtMarginPercent.Text = dbMarginPercent.ToString();
        }
        
        
        #endregion

        #region Total Information                
        public void CalculateTotalInformation()
        {
            double dbTotalBuyingPrice = 0;
            double dbMarginPercent = 0;
            double dbTotalPrice = 0;
            double dbDiscountAmount = 0;
            double dbTotalItemDiscountAmount = 0;
            double dbTotalItemNetAmount = 0;
            double dbTotalNetAmount = 0;
            double dbTotalTaxAmount = 0;
            double dbTotalGrossAmount = 0;

            Module.CurrentModuleEntity.CalculateTotalInformation(ref dbTotalPrice,
                                                                 ref dbTotalItemDiscountAmount,
                                                                 ref dbDiscountAmount,
                                                                 ref dbTotalTaxAmount,
                                                                 ref dbTotalBuyingPrice,
                                                                 ref dbTotalItemNetAmount,
                                                                 ref dbTotalNetAmount,
                                                                 ref dbTotalGrossAmount,
                                                                 ref dbMarginPercent);

            //Display
            fld_txtTotalItemNetAmounte.Text = dbTotalItemNetAmount.ToString();
            fld_txtDiscountAmount.Text = dbDiscountAmount.ToString();
            fld_txtTotalNetAmount.Text = dbTotalNetAmount.ToString();
            fld_txtTotalTaxAmount.Text = dbTotalTaxAmount.ToString();
            fld_txtTotalGrossAmount.Text = dbTotalGrossAmount.ToString();

            fld_txtTotalBuyingPrice.Text = dbTotalBuyingPrice.ToString();
            fld_txtTotalMarginPercent.Text = dbMarginPercent.ToString();

        }
        #endregion
        #endregion        
    }
}
